﻿Public Class OfficerIncome
    Inherits System.Web.UI.Page

    Private Property DataSource As Data.DataTable
        Get
            Return Me.Session("datasourceut619")
        End Get
        Set(ByVal value As Data.DataTable)
            Me.Session("datasourceut619") = value
        End Set
    End Property

    Private idxRow As Int32 = 0

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            BL.Report.SetMonthList(Me.ddlMonth)
            BL.Report.SetYearList(Me.ddlYear)
            LoadStatus()
        End If
    End Sub

    Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSearch.Click
        Dim dteFrom As String = Nothing
        Dim dteTo As String = Nothing
        Dim paidStatus As String = Nothing

        If Me.ddlPaidStatus.SelectedValue <> "-" Then
            paidStatus = Me.ddlPaidStatus.SelectedValue
        End If

        DataSource = BL.Officer.GetOfficerIncome(ddlMonth.SelectedValue, ddlYear.SelectedValue, txtTelSuccess.Text.Trim, ddlStatus.SelectedValue, Me.ucPopupOfficer1.OfficerAppId, paidStatus)

        Bind(0)
    End Sub

    Private Sub Bind(ByVal pageIndex As Integer)
        Me.gvList.DataSource = DataSource
        Me.gvList.PageIndex = pageIndex
        Me.gvList.DataBind()
    End Sub

    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnReset.Click
        Me.Session.Remove("datasourceut619")
        Response.Redirect("OfficerIncome.aspx", False)
    End Sub

    Private Sub gvList_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvList.PageIndexChanging
        Bind(e.NewPageIndex)
    End Sub

    Public Shared Function OrderNuber() As String
        Return String.Empty
    End Function

    Private Sub LoadStatus()
        Me.ddlStatus.DataSource = BL.OfficerStatus.AllStatus
        Me.ddlStatus.DataTextField = "STATUS_DESC"
        Me.ddlStatus.DataValueField = "STATUS_ID"
        Me.ddlStatus.DataBind()
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click
        Try
            Dim ofcInc As New List(Of Entity.OfficerIncome)

            For iRow As Int16 = 0 To gvList.Rows.Count - 1
                Dim grdRow As GridViewRow = gvList.Rows(iRow)

                Dim lblSumId As Label = CType(grdRow.FindControl("lblSumId"), Label)
                Dim lblPaid As Label = CType(grdRow.FindControl("lblPaid"), Label)
                Dim chkID As CheckBox = CType(grdRow.FindControl("chkPaid"), CheckBox)
                If (chkID.Checked AndAlso lblPaid.Text.Trim = "N") OrElse (Not chkID.Checked AndAlso lblPaid.Text.Trim = "Y") Then
                    Dim ofcIncDetail As Entity.OfficerIncome
                    ofcIncDetail = New Entity.OfficerIncome

                    If (chkID.Checked) Then
                        ofcIncDetail.income_paid_flag = "Y"
                    Else
                        ofcIncDetail.income_paid_flag = "N"
                    End If

                    ofcIncDetail.summary_id = Convert.ToInt64(lblSumId.Text.Trim)
                    ofcIncDetail.upd_user_id = GlobalApp.Authen.offcier_id
                    ofcIncDetail.upd_date = Now
                    ofcInc.Add(ofcIncDetail)
                End If
            Next

            Dim oResult As Boolean
            oResult = BL.Officer.UpdateOfficerIncome(ofcInc)

            If oResult Then
                If ofcInc.Count > 0 Then
                    Me.Master.Alert("SUCCESS\nบันทึกการจ่ายเงินเสร็จสมบูรณ์")
                Else
                    Me.Master.Alert("กรุณาเลือกรายการที่ต้องการจ่ายเงิน!")
                End If
            Else
                Me.Master.Alert("FAIL!\nบันทึกรายการไม่สำเร็จ รองใหม่อีกครั้ง")
            End If

        Catch ex As Exception
            Me.Master.Alert(ex.Message)
        End Try
    End Sub

    Private Sub gvList_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvList.RowDataBound
        Try
            Dim lblID As Label = CType(e.Row.FindControl("lblPaid"), Label)
            Dim chkID As CheckBox = CType(e.Row.FindControl("chkPaid"), CheckBox)
            If lblID.Text = "N" Then
                chkID.Checked = False
            Else
                chkID.Checked = True
            End If

            idxRow += 1
        Catch ex As Exception

        End Try
    End Sub

End Class